package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.Comment;
import com.example.service.CommentService;
import com.example.mapper.CommentMapper;
import com.example.service.UserService;
import com.example.utils.BeanCopyUtils;
import com.example.utils.ResponseResult;
import com.example.vo.CommentVo;
import com.example.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static com.example.utils.SecurityUtils.getUserId;

/**
* @author sprite-pc
* @description 针对表【sg_comment(评论表)】的数据库操作Service实现
* @createDate 2023-12-19 22:38:13
*/
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment>
    implements CommentService{

    @Autowired
    private UserService userService;

    @Autowired
    private CommentMapper commentMapper;



    @Override
    public ResponseResult commentList(String type, int pageNum, int pageSize, int articleId) {
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getArticleId,articleId);
        queryWrapper.eq(Comment::getType,type);
        queryWrapper.orderByDesc(Comment::getCreateTime);
        queryWrapper.eq(Comment::getRootId,-1);
//        List<Comment> comments = this.list(queryWrapper);
//        PageVo pageVo = new PageVo(pageNum,pageSize);
        Page<Comment> page = new Page<>(pageNum,pageSize);
        page(page,queryWrapper);
        Page<CommentVo> pageInfo = new Page<>(pageNum,pageSize);
        pageInfo.setRecords(BeanCopyUtils.copyBeanList(page.getRecords(), CommentVo.class));
        List<CommentVo> collect = pageInfo.getRecords().stream().map(commentVo -> {
//            commentVo.setToCommentUserName(userService.getById(commentVo.getToCommentId()).getUserName());
            commentVo.setUsername(userService.getById(commentVo.getCreateBy()).getUserName());
            LambdaQueryWrapper<Comment> queryWrapperChild = new LambdaQueryWrapper<>();
            queryWrapperChild.eq(Comment::getRootId,commentVo.getId());
            List<Comment> childs = commentMapper.selectList(queryWrapperChild);
            List<CommentVo> collect1 = childs.stream().map(child -> {
                CommentVo childVo = BeanCopyUtils.copyBean(child, CommentVo.class);
                childVo.setToCommentUserName(userService.getById(childVo.getToCommentUserId()).getUserName());
                childVo.setUsername(userService.getById(childVo.getCreateBy()).getUserName());
                return childVo;
            }).collect(Collectors.toList());
            commentVo.setChildren(collect1);

            return commentVo;
        }).collect(Collectors.toList());
        pageInfo.setRecords(collect);
        PageVo pageVo = new PageVo(pageInfo.getRecords(), (long) pageInfo.getRecords().size());
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult comment(Comment comment) {
        comment.setCreateTime(new Date());
        comment.setCreateBy(getUserId());
        save(comment);
        return ResponseResult.okResult();
    }
}




